﻿package com.wongoing.sys.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wongoing.base.BaseServiceImpl;
import com.wongoing.base.PageResult;
import com.wongoing.bus.util.Constants;
import com.wongoing.sys.dao.SysPushMessageDao;
import com.wongoing.sys.model.SysPushMessage;
import com.wongoing.sys.service.SysPushMessageService;
import com.wongoing.util.Jpush;
import com.wongoing.util.LogService;
@Transactional
@Service("sysPushMessageService")
public class SysPushMessageServiceImpl extends BaseServiceImpl<SysPushMessage> implements SysPushMessageService {
	private SysPushMessageDao dao;

	@Autowired
	public void setDao(SysPushMessageDao dao) {
		super.setDao(dao);
		this.dao = dao;
	}
	
	@Override
	public PageResult<SysPushMessage> getPageResultByParam(PageResult<SysPushMessage> pageResult) {
		pageResult.getParam().put("offset",
				pageResult.getPageSize() * (pageResult.getPageCurrent() - 1));
		pageResult.getParam().put("limit", pageResult.getPageSize());
		if (!"".equals(pageResult.getOrderField())) {
			pageResult.getParam().put("orderColumn", pageResult.getOrderField());
		}
		pageResult.getParam().put("orderTurn", pageResult.getOrderDirection());
		List<SysPushMessage> data = dao.getPushMessageByParam(pageResult.getParam());
		pageResult.setList(data);
		int totalSize = dao.countOfPushMessageByParam(pageResult.getParam());
		pageResult.setTotal(totalSize);
		return pageResult;
	}

	@Override
	public void sendPushNotice(String alia, String pushType,String content,Integer customerId, Map<String, String> params)
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		try {
			String sendPushNoticeTitle = null;
		    String[] alias=new String[1];
			//设置别名
		    alias[0]=alia;
			/**
			 * 推送-测试，正式需开启
			 */
//			if(pushType==Constants.CPUSH_CAR_ACCEPT_TEMPID) {
//				content = String.format(content, params.get("customerName"));
//				sendPushNoticeTitle="礼宾车预约成功";
//			}
			
			//将推送消息保存到推送消息表中
			SysPushMessage pushMs=new SysPushMessage();
			pushMs.setCustomerId(customerId);
			pushMs.setCustomerMobile(alia);
			pushMs.setTitle(sendPushNoticeTitle);
			pushMs.setStatus("0");
			pushMs.setIsRead(1);
			pushMs.setPushTime(new Date());
			pushMs.setContent(content);
			dao.insert(pushMs);
			//推送消息
			Jpush.sendPushNotice(alias,null,sendPushNoticeTitle,null,content);
			LogService.getInstance(this).debug("推送成功!");
			}
		 catch(Exception e) {
			LogService.getInstance(this).debug("推送失败!"+e.getMessage());
		 }
		
	}
}
